#include "SVPWM.h"

uint16_t Motor_PWM_Period;    	// 20KHz 对应 1600

/*
    PO1:HO1    P06:LO1
    P04:HO2    P07:LO2
    P05:HO3    P47:LO3
 */
void SVPWM_Init(uint16_t freq)
{
	/*(1)配置 EPWM 时钟分频，设置预分频比和独立分频比*/
	SYS_EnablePeripheralClk(SYS_CLK_EPWM_MSK);	
	EPWM_ConfigChannelClk(EPWM0, EPWM_CLK_DIV_1);
	EPWM_ConfigChannelClk(EPWM1, EPWM_CLK_DIV_1);
	EPWM_ConfigChannelClk(EPWM2, EPWM_CLK_DIV_1);	
	EPWM_ConfigChannelClk(EPWM3, EPWM_CLK_DIV_1);
	EPWM_ConfigChannelClk(EPWM4, EPWM_CLK_DIV_1);	
	EPWM_ConfigChannelClk(EPWM5, EPWM_CLK_DIV_1);
	/*(2)选择模式(互补模式)*/
    EPWM_ConfigRunMode( EPWM_COUNT_UP_DOWN  	|  		/*上下计数（中心对齐）模式*/
						EPWM_OCU_SYMMETRIC 		|		/*对称模式*/
						EPWM_WFG_COMPLEMENTARYK	|		/*互补模式*/
						EPWM_OC_INDEPENDENT);	        /*不用组控模式，独立输出模式*/
	/*(3)设置 EPWM 周期和占空比*/
    Motor_PWM_Period = PWM_CLK/freq/2; 
	EPWM_ConfigChannelPeriod(EPWM0,  Motor_PWM_Period);				
	EPWM_ConfigChannelPeriod(EPWM1,  Motor_PWM_Period);			
	EPWM_ConfigChannelPeriod(EPWM2,  Motor_PWM_Period);				
	EPWM_ConfigChannelPeriod(EPWM3,  Motor_PWM_Period);				
	EPWM_ConfigChannelPeriod(EPWM4,  Motor_PWM_Period);				
	EPWM_ConfigChannelPeriod(EPWM5,  Motor_PWM_Period);				
	EPWM_ConfigChannelSymDuty(EPWM0, 0);				
	EPWM_ConfigChannelSymDuty(EPWM1, 0);					
	EPWM_ConfigChannelSymDuty(EPWM2, 0);				
	EPWM_ConfigChannelSymDuty(EPWM3, 0);				
	EPWM_ConfigChannelSymDuty(EPWM4, 0);				
	EPWM_ConfigChannelSymDuty(EPWM5, 0);					
	/*(4)设置 EPWM 输出极性*/
	EPWM_DisableReverseOutput(EPWM_CH_0_MSK | EPWM_CH_1_MSK | EPWM_CH_2_MSK|
							  EPWM_CH_3_MSK | EPWM_CH_4_MSK | EPWM_CH_5_MSK);
	/*(5)设置EPWM加载方式*/
	EPWM_EnableAutoLoadMode(EPWM_CH_0_MSK | EPWM_CH_1_MSK |
							EPWM_CH_2_MSK | EPWM_CH_3_MSK |
							EPWM_CH_4_MSK | EPWM_CH_5_MSK);				/*设置为自动加载模式*/
	EPWM_ConfigLoadAndIntMode(EPWM0, EPWM_EACH_PERIOD_ZERO);			
	EPWM_ConfigLoadAndIntMode(EPWM1, EPWM_EACH_PERIOD_ZERO);
	EPWM_ConfigLoadAndIntMode(EPWM2, EPWM_EACH_PERIOD_ZERO);
	EPWM_ConfigLoadAndIntMode(EPWM3, EPWM_EACH_PERIOD_ZERO);
	EPWM_ConfigLoadAndIntMode(EPWM4, EPWM_EACH_PERIOD_ZERO);
	EPWM_ConfigLoadAndIntMode(EPWM5, EPWM_EACH_PERIOD_ZERO);			/*加载点设置为每个周期点和零点*/	
	EPWM_EnableDeadZone(EPWM_CH_0_MSK | EPWM_CH_1_MSK, 96);	/*死区时间0.5us*/
	EPWM_EnableDeadZone(EPWM_CH_2_MSK | EPWM_CH_3_MSK, 96);	/*死区时间0.5us*/
	EPWM_EnableDeadZone(EPWM_CH_4_MSK | EPWM_CH_5_MSK, 96);	/*死区时间0.5us*/
	EPWM_AllBrakeDisable();
	/*(6)设置中断及优先级*/
	EPWM_EnableZeroInt(EPWM_CH_0_MSK);		// 开启零点中断
	NVIC_SetPriority(EPWM_IRQn, 0);	/*优先级0~3， 0最高、3最低*/
	NVIC_EnableIRQ(EPWM_IRQn);
	/*(7)配置相关 IO 口为 EPWM 功能口*/	
	SYS_SET_IOCFG(IOP01CFG, SYS_IOCFG_P01_EPWM0);	
	SYS_SET_IOCFG(IOP04CFG, SYS_IOCFG_P04_EPWM1);
	SYS_SET_IOCFG(IOP05CFG, SYS_IOCFG_P05_EPWM2);
	SYS_SET_IOCFG(IOP06CFG, SYS_IOCFG_P06_EPWM3);
	SYS_SET_IOCFG(IOP07CFG, SYS_IOCFG_P07_EPWM4);
	SYS_SET_IOCFG(IOP47CFG, SYS_IOCFG_P47_EPWM5);
	/*(8)根据实际硬件电路进行重映射*/	
	EPWM->LOCK = EPWM_LOCK_P1B_WRITE_KEY;	
	EPWM->POREMAP = 0xAA531420;
	EPWM->LOCK = 0;
	
	/*
	(7)设置EPWM刹车
	*/
	
	EPWM_EnableFaultBrake(EPWM_BRK_ACMP1EE);	
	
	
	EPWM_ConfigFaultBrakeLevel(EPWM_CH_0_MSK | EPWM_CH_2_MSK | EPWM_CH_4_MSK, 0);
	EPWM_ConfigFaultBrakeLevel(EPWM_CH_1_MSK | EPWM_CH_3_MSK | EPWM_CH_5_MSK, 0);
	
	/*(9)使能相关 EPWM 通道输出*/	
    EPWM_EnableOutput(EPWM_CH_0_MSK | EPWM_CH_1_MSK|
					  EPWM_CH_2_MSK | EPWM_CH_3_MSK|
					  EPWM_CH_4_MSK | EPWM_CH_5_MSK);	
	EPWM_Start(EPWM_CH_0_MSK | EPWM_CH_1_MSK |
			   EPWM_CH_2_MSK | EPWM_CH_3_MSK |
			   EPWM_CH_4_MSK | EPWM_CH_5_MSK );
}
